﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MusicList : System.Web.UI.Page
{
    static int pageSize = 7;
    int gid;
    int pid;

    protected void Page_Load(object sender, EventArgs e)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        string gid_s = Request.QueryString["gid"];
        bool t = Int32.TryParse(gid_s, out gid);
        string pid_s = Request.QueryString["pid"];
        bool t1 = Int32.TryParse(pid_s, out pid);

        if ((t1 && pid <= 0) || (!t1))
        {
            pid = 1;
        }

        if (t)
        {
            LoadGenresMusic(gid, pid);            
        }
        else
        {
            LoadAllMusic(pid);
        }
    }

    private void LoadAllMusic(int pid)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        titleDetails.InnerText = "Music";

        var musiclst = (from i in db.Musics
                        select new
                        {
                            MusicID = i.MusicID,
                            Title = i.Title,
                            AlbumID = i.AlbumID,
                            AlbumName = i.Album.AlbumName,
                            ArtistID = i.Album.ArtistID,
                            ArtistName = i.Album.Artist.ArtistName,
                            Rating = i.Rating,
                            Cover = i.Cover,
                            Date = i.Album.Date
                        }).OrderByDescending(i => i.Date).Skip((pid - 1) * pageSize).Take(pageSize);        

        Rpt_MusicList.DataSource = musiclst;
        Rpt_MusicList.DataBind();

        int numItems = db.Musics.Count();
        int numPages = numItems / pageSize;
        if (numItems % pageSize != 0)
        {
            numPages++;
        }
        BuildPaggingControls(numPages);
    }

    private void LoadGenresMusic(int gid, int pid)
    {
        Sharemp3DataContext db = new Sharemp3DataContext();

        string genresname = db.Genres.Where(i => i.GenresID == gid).First().GenresName;
        titleDetails.InnerText = genresname + " Music";

        var musiclst = (from i in db.Musics
                       where i.GenresID == gid
                       select new
                       {
                           MusicID = i.MusicID,
                           Title = i.Title,
                           AlbumID = i.AlbumID,
                           AlbumName = i.Album.AlbumName,
                           ArtistID = i.Album.ArtistID,
                           ArtistName = i.Album.Artist.ArtistName,
                           Rating = i.Rating,
                           Cover = i.Cover,
                           Date = i.Album.Date
                       }).OrderByDescending(i => i.Date).Skip((pid - 1) * pageSize).Take(pageSize);;

        Rpt_MusicList.DataSource = musiclst;
        Rpt_MusicList.DataBind();

        int numItems = db.Musics.Where(i => i.GenresID == gid).Count();
        int numPages = numItems / pageSize;
        if (numItems % pageSize != 0)
        {
            numPages++;
        }
        BuildPaggingControlsOfGenres(gid, numPages);
    }

    private void BuildPaggingControls(int numPages)
    {
        paggingControls.Controls.Clear();
        if (numPages > 1)
        {
            for (int i = 1; i <= numPages; i++)
            {
                LinkButton lnk = new LinkButton();
                lnk.Text = " " + i + " ";
                lnk.PostBackUrl = "MusicList.aspx?pid=" + i;
                paggingControls.Controls.Add(lnk);
            }
        }
    }

    private void BuildPaggingControlsOfGenres(int gid, int numPages)
    {
        paggingControls.Controls.Clear();
        if (numPages > 1)
        {
            for (int i = 1; i <= numPages; i++)
            {
                LinkButton lnk = new LinkButton();
                lnk.Text = i.ToString();
                lnk.PostBackUrl = "MusicList.aspx?gid=" + gid + "&pid=" + i;
                paggingControls.Controls.Add(lnk);
            }
        }
    }
}